package com.jn.rxjava.views;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log; // 导入Log类
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.jn.rxjava.R;
import com.jn.rxjava.adapter.RetrofitClient;
import com.jn.rxjava.service.ApiService;
import com.jn.rxjava.service.NewsResponse;

import java.io.Serializable;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class NewsActivity extends AppCompatActivity {
    private Spinner typeSpinner;
    private EditText pageEditText, pageSizeEditText, isFilterEditText;
    private Button fetchButton;
    private ApiService apiService;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);

        typeSpinner = findViewById(R.id.type_spinner);
        pageEditText = findViewById(R.id.page_edit_text);
        pageSizeEditText = findViewById(R.id.page_size_edit_text);
        isFilterEditText = findViewById(R.id.is_filter_edit_text);
        fetchButton = findViewById(R.id.fetch_button);

        apiService = RetrofitClient.getRetrofitInstance().create(ApiService.class);

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.news_types, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        typeSpinner.setAdapter(adapter);

        fetchButton.setOnClickListener(v -> {
            String type = typeSpinner.getSelectedItem().toString();
            String page = pageEditText.getText().toString().trim();
            String pageSize = pageSizeEditText.getText().toString().trim();
            String isFilter = isFilterEditText.getText().toString().trim();

            fetchNewsData(type, page, pageSize, isFilter);
        });
    }

    private void fetchNewsData(String type, String page, String pageSize, String isFilter) {
        Disposable disposable = apiService.getNews("83e843ec35909a916b888fef1db6e9ea", type, Integer.parseInt(page), Integer.parseInt(pageSize), Integer.parseInt(isFilter))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response -> {
                    if (response.getError_code() == 0) {
                        // 向新页面传递新闻数据
                        Intent intent = new Intent(NewsActivity.this, NewsListActivity.class);
                        intent.putExtra("newsData", (Serializable) response.getResult().getData());
                        startActivity(intent);
                    } else {
                        String error = "获取失败: " + response.getReason();
                        Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
                        Log.e("NewsActivity", error); // 添加日志
                    }

                }, throwable -> {
                    String errorMessage = "获取失败: " + throwable.getMessage();
                    Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show();
                    Log.e("NewsActivity", errorMessage); // 添加日志
                });
    }
}
